<?php
namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Form\UploadForm;
use Album\Form\MultiUploadForm;
use Zend\Mvc\Controller\Plugin\FlashMessenger;

class UploadController extends AbstractActionController
{

    public function multiAction(){

        $form = new MultiUploadForm();

        $request = $this->getRequest();
        if ($request->isPost()) {

            $messages = 'Tap tin vua upload len server la: ';
            $files = $form->upload($request->getFiles()->toArray(), FILES_PATH);
            $messages .= implode(", ", $files);

            $this->flashMessenger()->addMessage($messages);

var_dump($this->flashMessenger()->getMessages()); die;

            return $this->redirect()->toRoute('album/upload', array('controller'=>'upload', 'action'=>'multi'));
        }

        $view = new ViewModel(array('form'=>$form,
            'messages' => $this->flashMessenger()->getMessages()
        ));

        return $view;
    }

    public function indexAction()
    {
       $form = new UploadForm();
       $view = new ViewModel(array('form'=>$form));
       $request = $this->getRequest();

       if ($request->isPost()) {

           $files =  $request->getFiles()->toArray();
           $fileName = $files['picture']['name'];

           $uploadObj = new \Zend\File\Transfer\Adapter\Http();
           $uploadObj->setDestination(FILES_PATH);

           if($uploadObj->receive($fileName)) {
               $this->flashMessenger()->addMessage("Upload file: [{$fileName}] thành công!");

               return $this->redirect()->toRoute('album/upload', array('controller'=>'upload', 'action'=>'index'));
           }

       }

       return $view;
    }

    public function abcAction() {
        $messages = '';

        $form = new UploadForm();

        $request = $this->getRequest();
        if ($request->isPost()) {
            $messages = 'Tap tin vua upload len server la: ';
            $messages .= $form->upload($request->getFiles()->toArray(), FILES_PATH);

            $this->flashMessenger()->addMessage($messages);

            return $this->redirect()->toRoute('album/upload', array('controller'=>'upload', 'action'=>'abc'));
        }

        $view = new ViewModel(array('form'=>$form,
            'messages' => $this->flashMessenger()->getMessages()
        ));

        return $view;
    }

}